package com.sbr.seb.wicket.page.administration;

import java.util.List;

import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sbr.seb.entity.composants.Category;
import com.sbr.seb.entity.composants.Item;
import com.sbr.seb.planning.AdministrationService;
import com.sbr.seb.planning.dao.CategoryDao;

public class AddItemPage extends AdministrationBasePage {

	private static final long serialVersionUID = 1L;
	protected static final Logger logger = LoggerFactory.getLogger(AddItemPage.class);
	
	private Item item;
	
	@SpringBean
	private AdministrationService administrationService;
	
	@SpringBean
	private CategoryDao categoryDao;
	
	
	public AddItemPage() {
		super();
		item = new Item();

		final TextField<Object> itemNameText = new RequiredTextField("itemName", new PropertyModel(item, "name"));
		final TextField<Object> itemDescriptionText = new TextField("itemDescription",new PropertyModel(item, "description"));

		final DropDownChoice<Category> itemCategoryBox = new DropDownChoice<Category>("itemCategoryBox", new PropertyModel(item,"category"), new LoadableDetachableModel<List<Category>>(){
			@Override
			protected List<Category> load() { 
				return categoryDao.findAll();
			}
		},  new ChoiceRenderer<Category>("name", "id"));

		
		Form form = new Form("addItemForm") {

			@Override
			protected void onSubmit() {
				logger.debug("onSubmit called");
				administrationService.addItem(item);
			}
		};
		form.add(itemNameText);
		form.add(itemDescriptionText);
		form.add(itemCategoryBox);
		
		add(form);
		add(new FeedbackPanel("feedbackPanel"));
	}

	public void setAdministrationService(AdministrationService administrationService) {
		this.administrationService = administrationService;
	}
	
}
